package com.easygo.controller;

import com.easygo.client.ContentClient;
import com.easygo.client.GoodsClient;
import com.easygo.client.ItemCatClient;
import com.easygo.pojo.Content;
import com.easygo.pojo.Goods;
import com.easygo.pojo.ItemCat;
import org.apache.commons.collections.ListUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 徐策
 * @date 2020/1/4 15:27
 */
@Controller
@Scope("prototype")
public class IndexController {
    @Resource
    ItemCatClient itemCatClient;

    @Resource
    ContentClient contentClient;

    @Resource
    GoodsClient goodsClient;

    @Resource
    RedisTemplate redisTemplate;

    @GetMapping(value = {"/", "/index", "index.html"})
    public String index(Model model) {
        createItemCatData(model);   //生成商品分类数据
        getContents(model); //查询广告
        create1to6FGoodsData(model);    //获得1到6楼的商品信息
        return "index";  //跳转到index模板上
    }



    /**
     * 01-生成首页的分类数据(数据整理)
     *
     * @return
     */
    private void createItemCatData(Model model) {
        //所有的分类

        List<ItemCat> itemCatList = (List<ItemCat>) redisTemplate.boundHashOps("mydata").get("itemCatList");
        if(itemCatList==null){
            itemCatList = itemCatClient.getItemCatList();
            System.out.println("走的是数据库");
            redisTemplate.boundHashOps("mydata").put("itemCatList",itemCatList);
        }else {
            System.out.println("走的是缓存");
        }
        //一级分类
        List<ItemCat> itemCatOne = getItemCatOne(itemCatList);
        //二级分类
        List<ItemCat> itemCatSecond = getItemCatSecond(itemCatOne, itemCatList);
        //三级分类
        List<ItemCat> itemCatThird = getItemCatSecond(itemCatSecond, itemCatList);
        model.addAttribute("itemCatOne",itemCatOne);
        model.addAttribute("itemCatSecond",itemCatSecond);
        model.addAttribute("itemCatThird",itemCatThird);
    }


    /**
     * 02-所有的二级分类
     * @param itemCatOne
     * @param itemCatList
     * @return
     */
    public List<ItemCat> getItemCatSecond(List<ItemCat> itemCatOne,List<ItemCat> itemCatList){
        List<ItemCat> twos = new ArrayList<ItemCat>();
        for (ItemCat one : itemCatOne) {
            for (ItemCat all : itemCatList) {
                if(one.getId().toString().equals((all.getParentId().toString()))){
                    twos.add(all);
                }
            }
        }
        return twos;
    }

    /**
     * 01-获取所有的一级分类
     * @param itemCatList
     * @return
     */
    public List<ItemCat> getItemCatOne(List<ItemCat> itemCatList) {
        List<ItemCat> ones = new ArrayList<ItemCat>();
        int count=0;
        for (ItemCat itemCat : itemCatList) {
            if (itemCat.getParentId().toString().equals("0")) {
                ones.add(itemCat);
                count++;
                if(count==10){
                    break;
                }
            }
        }
        return ones;
    }

    /**
     * 查询广告
     *
     * @param model
     */
    private void getContents(Model model) {
        //缓存如果有查询缓存，缓存没有查询数据库(然后再存入一份缓存，下次查询缓存)
        List<Content> lunboImages= (List<Content>) redisTemplate.boundHashOps("mydata").get("lunboImages");
        List<Content> shengxianImages= (List<Content>) redisTemplate.boundHashOps("mydata").get("shengxianImages");
        if (lunboImages==null){
            System.out.println("缓存中没有轮播图的数据,需要查询一次数据库~");
            //所有的分类
            lunboImages = contentClient.getContentsByCategoryId(1L);//轮播图
            redisTemplate.boundHashOps("mydata").put("lunboImages", lunboImages);//存入缓存
        }else{
            System.out.println("缓存中有轮播图数据，走缓存....");
        }
        if(shengxianImages==null){
            //所有的分类
            System.out.println("缓存中没有生鲜的数据,需要查询一次数据库~");
            shengxianImages = contentClient.getContentsByCategoryId(10L);//轮播图
            redisTemplate.boundHashOps("mydata").put("shengxianImages", shengxianImages);//存入缓存
        }else{
            System.out.println("缓存中有生鲜图数据，走缓存....");
        }
        model.addAttribute("lunboImages", lunboImages);
        model.addAttribute("shengxianImages", shengxianImages);
    }


    /**
     * 通过category1id获得1~6F类别的六种商品数据
     */
    public void create1to6FGoodsData(Model model){
        //设置要获取的商品类别id
        Long floor_1_freshFood_goods = 1218L;       //生鲜
        Long floor_2_drink_goods =  378L;           //食品和饮料
        Long floor_3_makeup_goods = 249L;           //个人美妆
        Long floor_4_momAndBabe_goods = 296L;       //母婴
        Long floor_4_toy_goods = 495L;              //玩具
        Long floor_5_furniture_goods = 633L;        //家具
        Long floor_6_digit_goods = 558L;             //手机
        Long floor_6_houseEleApp_goods = 1206L;     //家电

        List<Goods> floor1Goods = goodsClient.getGoodsByCategory1Id(floor_1_freshFood_goods);
        List<Goods> floor2Goods = goodsClient.getGoodsByCategory1Id(floor_2_drink_goods);
        List<Goods> floor3Goods = goodsClient.getGoodsByCategory1Id(floor_3_makeup_goods);
        List<Goods> floor4Goods = ListUtils.union(goodsClient.getGoodsByCategory1Id(floor_4_momAndBabe_goods)
                                                , goodsClient.getGoodsByCategory1Id(floor_4_toy_goods));

        List<Goods> floor5Goods = goodsClient.getGoodsByCategory1Id(floor_5_furniture_goods);
        List<Goods> floor6Goods = ListUtils.union(goodsClient.getGoodsByCategory1Id(floor_6_digit_goods)
                                                , goodsClient.getGoodsByCategory1Id(floor_6_houseEleApp_goods));
        model.addAttribute("floor1Goods",floor1Goods);
        model.addAttribute("floor2Goods",floor2Goods);
        model.addAttribute("floor3Goods",floor3Goods);
        model.addAttribute("floor4Goods",floor4Goods);
        model.addAttribute("floor5Goods",floor5Goods);
        model.addAttribute("floor6Goods",floor6Goods);
    }


}
